package cn.iocoder.yudao.framework.common.object;

import java.util.List;

public class ExpirableObject<T> {
    private final T data;
    private final long expireAt;

    public ExpirableObject(T data, long ttlMillis) {
        this.data = data;
        this.expireAt = System.currentTimeMillis() + ttlMillis;
    }

    public T get() {
        if (System.currentTimeMillis() > expireAt) {
            return null;
        }
        return data;
    }

    public static void main(String[] args) {
        // 例子
        List<String> data = List.of("item1", "item2", "item3");
        ExpirableObject<List<String>> expirableObject = new ExpirableObject<>(data, 2000); // 2秒后过期

        // 获取数据
        List<String> items = expirableObject.get();
        System.out.println("Items: " + items);

        // 等待2秒后再次获取
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 再次获取
        items = expirableObject.get();
        System.out.println("Items after expiration: " + items);
    }
}